
#include <BEntry>
#include <BFolder>
#include <BReadWrite>
#include <BIcon>
#include "member_BIcon.h"

using namespace BWE;

#define member					(*(member_BIcon*)_ptr)
#define member_allocate()		_ptr = new member_BIcon(this)
#define member_release()		delete (member_BIcon*)_ptr

BIcon::BIcon()
{
	member_allocate();
}
BIcon::BIcon(BImage* image)
{
	member_allocate();
	member.images[State_None] = image;
	member.checkedImages[State_None] = image;
}
BIcon::BIcon(const BString& fileName)
{
	member_allocate();
	reset(fileName);
}
BIcon::BIcon(const BIcon& other)
{
	member_allocate();
	for (int i = State_None; i < State_Max; i++)
	{
		member.images[i] = icon_member(&other)->images[i];
		member.checkedImages[i] = icon_member(&other)->checkedImages[i];
	}
}
BIcon::~BIcon()
{
	member_release();
}

bool BIcon::reset(const BString& fileName)
{
	clear();
	if (fileName.ext())
	{
		BImageHolder image = ReadFile<BImage>(fileName);
		member.images[State_None] = image;
		member.checkedImages[State_None] = image;
		return false;
	}
	BFolder folder(fileName.path());
	if (!folder.open())
		return false;
	BString name = fileName.name();
	for (int i = 0; i < folder.entryCount(); i++)
	{
		BEntry* entry = folder.entry(i);
		if (entry->attrib() != Attrib_File)
			continue;
		const BString& entryName = entry->name();
		if (!entryName.beginWith(name) || !entryName.ext())
			continue;
		if (entryName.contain("-checked") || entryName.contain("-check"))
		{
			if (entryName.contain("-normal"))
			{
				member.checkedImages[State_Normal] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
			if (entryName.contain("-hover") || entryName.contain("-hovered"))
			{
				member.checkedImages[State_Hovered] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
			if (entryName.contain("-press") || entryName.contain("-pressed"))
			{
				member.checkedImages[State_Pressed] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
			if (entryName.contain("-disable") || entryName.contain("-disabled"))
			{
				member.checkedImages[State_Disable] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
			if (entryName.contain("-select") || entryName.contain("-selected"))
			{
				member.checkedImages[State_Selected] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
			BString pureName = entryName;
			pureName -= entryName.ext();
			pureName -= "-checked.";
			pureName -= "-check.";
			if (pureName == fileName.name())
			{
				member.checkedImages[State_None] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
		}
		else
		{
			if (entryName.contain("-normal"))
			{
				member.images[State_Normal] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
			if (entryName.contain("-hover") || entryName.contain("-hovered"))
			{
				member.images[State_Hovered] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
			if (entryName.contain("-press") || entryName.contain("-pressed"))
			{
				member.images[State_Pressed] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
			if (entryName.contain("-disable") || entryName.contain("-disabled"))
			{
				member.images[State_Disable] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
			if (entryName.contain("-select") || entryName.contain("-selected"))
			{
				member.images[State_Selected] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
			BString pureName = entryName;
			pureName -= entryName.ext();
			pureName -= '.';
			if (pureName == fileName.name())
			{
				member.images[State_None] = ReadFile<BImage>(folder.path() + '/' + entry->name());
				continue;
			}
		}
	}
	if (member.images[State_Normal] == 0)
		member.images[State_Normal] = member.images[State_None];
	if (member.checkedImages[State_None] == 0)
		member.checkedImages[State_None] = member.images[State_None];
	if (member.checkedImages[State_Normal] == 0)
		member.checkedImages[State_Normal] = member.checkedImages[State_None];
	return true;
}

void BIcon::setImage(BImage* image, State state, bool checked)
{
	if (state >= State_None && state < State_Max)
	{
		if (checked)
			member.checkedImages[state] = image;
		else
			member.images[state] = image;
	}
}
void BIcon::setImage(const BString& imageFileName, State state, bool checked)
{
	if (state >= State_None && state < State_Max)
	{
		BImage* image = ReadFile<BImage>(imageFileName);
		if (checked)
			member.checkedImages[state] = image;
		else
			member.images[state] = image;
	}
}

const BImage* BIcon::image(State state, bool checked) const
{
	if (state >= State_None && state < State_Max)
	{
		if (checked)
		{
			if (member.checkedImages[state] == 0)
				member.makeCheckedImage(state);
			return member.checkedImages[state];
		}
		if (member.images[state] == 0)
			member.makeStateImage(state);
		return member.images[state];
	}
	return 0;
}
BImage* BIcon::image(State state, bool checked)
{
	if (state >= State_None && state < State_Max)
	{
		if (checked)
		{
			if (member.checkedImages[state] == 0)
				member.makeCheckedImage(state);
			return member.checkedImages[state];
		}
		if (member.images[state] == 0)
			member.makeStateImage(state);
		return member.images[state];
	}
	return 0;
}

const BImage* BIcon::image(bool checked) const
{
	if (checked)
		return member.checkedImages[0];
	return member.images[0];
}
BImage* BIcon::image(bool checked)
{
	if (checked)
		return member.checkedImages[0];
	return member.images[0];
}

bool BIcon::empty() const
{
	for (int i = State_None; i < State_Max; i++)
	{
		if (member.images[i])
			return false;
		if (member.checkedImages[i])
			return false;
	}
	return true;
}
void BIcon::clear()
{
	for (int i = 0; i < State_Max; i++)
	{
		member.images[i] = 0;
		member.checkedImages[i] = 0;
	}
}

